package org.examcity.domain;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.springframework.util.Assert;

/**
 * Entity that represents a knowledge subject and may contain a set of evaluation objectives.
 * 
 * @author Antonio Gomes
 */
@Entity
@Table(name = "EC_SUBJECT")
public class Subject {

	// ATTRIBUTES ----------------------------------------------------------------------------------

	@Id
	@Column(name = "SUBJECT_ID")
	@SequenceGenerator(name = "SEQ_EC_SUBJECT")
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private Long id;

	@Column(nullable = false, length = 50)
	private String name;

	@Column(length = 250)
	private String description;

	@OneToMany(mappedBy = "subject")
	private Set<Objective> objectives;

	// ACCESSORS -----------------------------------------------------------------------------------

	/**
	 * Gets an unmodifiable set with all objectives.
	 * <p>
	 * To add a new element you must use {@link Objective#setSubject(Subject)}.
	 * @return unmodifiable set
	 */
	public Set<Objective> getObjectives() {
		return (objectives == null) ? null : Collections.unmodifiableSet(objectives);
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	// OTHER METHODS -------------------------------------------------------------------------------

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(name).toHashCode();
	}

	@Override
	public boolean equals(Object _o) {
		if (_o instanceof Subject) {
			Subject o = (Subject) _o;
			return new EqualsBuilder().append(name, o.getName()).isEquals();
		}
		return false;
	}

	// HELPER METHODS ------------------------------------------------------------------------------

	/**
	 * Adds an unidirectional link to an {@link Objective}.
	 * <p>
	 * This method is supposed to be called only by {@link Objective#setSubject(Subject)}.
	 * @param o
	 */
	void _addObjective(Objective o) {
		objectives = (objectives == null) ? new HashSet<Objective>() : objectives;
		boolean wasNotContained = objectives.add(o);
		Assert.isTrue(wasNotContained, "Link to Objective already defined");
	}

	/**
	 * Removes an unidirectional link to an {@link Objective}.
	 * <p>
	 * This method is supposed to be called only by {@link Objective#setSubject(Subject)}.
	 * @param o
	 */
	void _removeObjective(Objective o) {
		objectives = (objectives == null) ? new HashSet<Objective>() : objectives;
		boolean wasContained = objectives.remove(o);
		Assert.isTrue(wasContained, "Missing link to Objective");
	}

}
